﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using S4Store.Domain.SellerCarBrand;
using S4Store.Domain.CarSales;
using S4Store.Domain.Report;
using S4Store.Domain.FinancialReport;
using S4Store.Domain.NetMarketingKpi;
using S4Store.Domain.ExhibitionKpi;

namespace S4Store.Models.Report
{
    public class IndexModel : BaseReportModel
    {
        public IndexModel():base()
        {

        }

        public CarSaleReport SaleReport { get; set; }
        public FinancialCommonReport FinancialReport { get; set; }
        public NetMarketingKpiReport NetMarketingKpiReport { get; set; }
        public ExhibitionReport ExhibitionReport { get; set; }
        public S4Store.Domain.Report.SellerReport SellerReport { get; set; }
        public S4Store.Domain.Report.SellerReport TelSellerReport { get; set; }

        public List<S4Store.Domain.AfterSaleKpi.AfterSaleCarTypeMonthConfig> AfterSaleCarTypeMonthConfigs { get; set; }
        public List<S4Store.Domain.AfterSaleKpi.AfterSaleCarTypeDayInput> AfterSaleCarTypeDayInputs { get; set; }
        public List<S4Store.Domain.AfterSaleKpi.MonthStandardConfig> AFStandardConfigs { get; set; }
        public List<S4Store.Domain.AfterSaleKpi.AfterSaleServiceAdvisorDayInput> AFServiceAdvisorDayInputs { get; set; }
        public List<S4Store.Domain.AfterSaleKpi.AfterSaleServiceAdvisorMonthTarget> AFServiceAdvisorMonthTargets { get; set; }
        public S4Store.Domain.AfterSaleKpi.SparePartsMonthInput SparePartsMonthInput { get; set; }
       

        /// <summary>
        /// 入厂台次
        /// </summary>
        public G1Item RCTC
        {
            get
            {
                var timeRate = (decimal)BarModel.Month.LastOrNowDay()/(decimal)BarModel.Month.LastDay();
                //入厂台次目标
                var RCTCMB = AfterSaleCarTypeMonthConfigs.Sum(o=>o.BYYSS5KBYS_MonthTarget5K+o.BYYSS10KBYS_MonthTarget10k+o.BYSGCRCTCMB_MonthAacidentCarToFactoryTarget+o.BYWXRCTCMB_MonthMaintainCarToFactoryTarget);
                //入厂台次实际
                var RCTCSJ = AfterSaleCarTypeDayInputs.Sum(o=>o.SGRCTC_AccidentCarToFactory+o.WXRCTC_MaintainCarToFactory+o.WKBYS_Maintain5KNum+o.SQBYS_Maintain10KNum);
                //应入厂台次
                var YRCTC = RCTCMB * timeRate;

                G1Item item = new G1Item();
                item.Name = "入厂台次(目标：" + RCTCMB + "台)";
                item.Ratio = (RCTCMB==0?0:(decimal)RCTCSJ / (decimal)RCTCMB)*100;
                item.Text = RCTCSJ.ToString()+"台";
                item.IsStandard = YRCTC < RCTCSJ;

                return item;
            }
        }

        /// <summary>
        /// 当前产值
        /// </summary>
        
        public G1Item DQCZ
        {
            get
            {
                var timeRate = (decimal)BarModel.Month.LastOrNowDay() / (decimal)BarModel.Month.LastDay();
                //产值目标
                var CZMB = AfterSaleCarTypeMonthConfigs.Sum(o => o.WQBYMBCZ_5KMaintenanceTargetValue + o.SQBYMBCZ_TenKMaintenanceTargetValue + o.BYSGCCZBM_MonthAacidentCarTargetValue + o.BYWXCZMB_MonthMaintenanceTargetValue);
                //产值实际
                var CZSJ = AfterSaleCarTypeDayInputs.Sum(o => o.WQBYCZ_Maintain5KValue + o.SQBYCZ_Maintain10KValue + o.QTWXCZ_MaintainOtherValue + o.SGCCZ_AccidentCarValue);
                //应该产值
                var YCZ = CZMB * timeRate;

                G1Item item = new G1Item();
                item.Name = "当前产值(目标：" + CZMB + "万元)";
                item.Ratio = (CZMB == 0 ? 0 : (decimal)CZSJ / (decimal)CZMB) * 100;
                item.Text = CZSJ.toInt().ToString() + "万元";
                item.IsStandard = YCZ < CZSJ;

                return item;
            }
        }


        /// <summary>
        /// 单车产值
        /// </summary>
        public G1Item DCCZ
        {
            get
            {
                var timeRate = (decimal)BarModel.Month.LastOrNowDay() / (decimal)BarModel.Month.LastDay();
                //产值目标
                var CZMB = AfterSaleCarTypeMonthConfigs.Sum(o => o.WQBYMBCZ_5KMaintenanceTargetValue + o.SQBYMBCZ_TenKMaintenanceTargetValue + o.BYSGCCZBM_MonthAacidentCarTargetValue + o.BYWXCZMB_MonthMaintenanceTargetValue);
                //产值实际
                var CZSJ = AfterSaleCarTypeDayInputs.Sum(o => o.WQBYCZ_Maintain5KValue + o.SQBYCZ_Maintain10KValue + o.QTWXCZ_MaintainOtherValue + o.SGCCZ_AccidentCarValue);

                //入厂台次目标
                var RCTCMB = AfterSaleCarTypeMonthConfigs.Sum(o => o.BYYSS5KBYS_MonthTarget5K + o.BYYSS10KBYS_MonthTarget10k + o.BYSGCRCTCMB_MonthAacidentCarToFactoryTarget + o.BYWXRCTCMB_MonthMaintainCarToFactoryTarget);
                //入厂台次实际
                var RCTCSJ = AfterSaleCarTypeDayInputs.Sum(o => o.SGRCTC_AccidentCarToFactory + o.WXRCTC_MaintainCarToFactory + o.WKBYS_Maintain5KNum + o.SQBYS_Maintain10KNum);

                var MBDCCZ = RCTCMB == 0 ? 0 : CZMB / RCTCMB;
                var SJDCCZ = RCTCSJ == 0 ? 0 : CZSJ / RCTCSJ;

                //应该产值
                var YDCCZ = MBDCCZ * timeRate;

                G1Item item = new G1Item();
                item.Name = "单车产值(目标：" + Math.Round( MBDCCZ,2,MidpointRounding.AwayFromZero) + "万元)";
                item.Ratio = (MBDCCZ == 0 ? 0 : (decimal)SJDCCZ / (decimal)MBDCCZ) * 100;
                item.Text = Math.Round(SJDCCZ, 2, MidpointRounding.AwayFromZero) + "万元";
                item.IsStandard = SJDCCZ > MBDCCZ;

                return item;
            }
        }

        /// <summary>
        /// 月度库存成本
        /// </summary>
        public G1Item BJKC
        {
            get
            {
                if (SparePartsMonthInput == null)
                {
                    return new G1Item()
                    {
                        IsStandard = false,
                        Name = "月度库存成本(目标：0万元)",
                        Ratio = 0,
                        Text = "0万元"
                    };
                }
                else
                {
                    G1Item item = new G1Item();
                    item.Name = "月度库存成本(目标：0万元)";
                    item.Ratio = 100;
                    item.Text = SparePartsMonthInput.YCKCCB.toInt().ToString()+"万元";
                    item.IsStandard = true;
                    return item;
                }

            }
        }

        /// <summary>
        /// 客户流失率
        /// </summary>
        public G1Item KHLSL
        {
            get
            {
                //六个月未回厂总数/
                S4Store.Models.AfterSaleReport.AfterSaleReportServiceAdvisorModel model1 = new S4Store.Models.AfterSaleReport.AfterSaleReportServiceAdvisorModel();
                model1.StandardConfigs = AFStandardConfigs;
                model1.SADayDatas = AFServiceAdvisorDayInputs;
                model1.MonthTargets = AFServiceAdvisorMonthTargets;
                decimal 目标客户流失率 = 0;
                if (model1.StandardConfigs.FirstOrDefault() != null) 目标客户流失率 = model1.StandardConfigs.FirstOrDefault().事实流失率目标;

                G1Item item = new G1Item();
                item.Name = "事实客户流失率(目标:" + 目标客户流失率 + "%)";
                item.Ratio = model1.z_SSKHLSL(BarModel.Month.Year, BarModel.Month.Month);
                item.Text = string.Format("{0}%", model1.z_SSKHLSL(BarModel.Month.Year, BarModel.Month.Month));
                item.IsStandard = model1.z_SSKHLSL_CY(BarModel.Month.Year, BarModel.Month.Month)>0;

                return item;
            }
        }
    }
}